class 接雨水 {
    public static void main(String[] args) {
        int trap = trap(new int[]{0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1});
        System.out.println(trap);
    }
    public static  int trap(int[] height) {
        int peakIndex=0;
        int peakHeight=0;
        for(int i=0;i<height.length;i++){
            if(height[i]>peakHeight){
                peakHeight=height[i];
                peakIndex=i;
            }
        }
        int water=0;
        //先看左边
        int leftMostBar=0;
        for (int i = 0; i <=peakIndex; i++) {
            if(height[i]>leftMostBar){
                leftMostBar=height[i];
            }
            else {
                water+=leftMostBar-height[i];
            }
        }
        //然后看右边
        int rightMostBar=0;
        for (int i =height.length-1; i>peakIndex ; i--) {
            if(height[i]>rightMostBar){
                rightMostBar=height[i];
            }
            else {
                water+=rightMostBar-height[i];
            }
        }
        return water;
    }
}